﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;

namespace System
{
    public class HotKeyHelper
    {
        /// <summary>
        /// Alt + F4 无效化处理
        /// </summary>
        /// <param name="window">Alt + F4 要无效的窗口</param>
        /// <returns>Alt + F4 无效化处理</returns>
        /// <remarks>返回Action是方便调用处需要同时进行很多处理时+=，-=用</remarks>
        public static Action DisableAltF4(Window window)
        {
            var source = HwndSource.FromHwnd(new WindowInteropHelper(window).Handle);
            source.AddHook(DisableAltF4WndHookProc);
            return () => source.RemoveHook(DisableAltF4WndHookProc);
        }

        /// <summary>
        /// Alt + F4无效化窗口消息
        /// </summary>
        private static IntPtr DisableAltF4WndHookProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            const int WM_SYSKEYDOWN = 0x0104;
            const int VK_F4 = 0x73;

            if (msg == WM_SYSKEYDOWN && wParam.ToInt32() == VK_F4)
            {
                handled = true;
            }

            return IntPtr.Zero;
        }

    }
}
